﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WFNetLib.USB.HID;

namespace 车钥匙
{
    public partial class HIDDevice : Win32Usb, IDisposable
    {
        public static byte[] DTnec=new byte[12]{0x00,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x23,0x01,0x26};
	    public static byte[] DTmot=new byte[12]{0x00,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x23,0x01,0x26};
        public HIDResult EZSReadDataStep(int step, EZSMode mode, out InteractionData rx)
        {
            InteractionData tx = new InteractionData();

            tx.buff[0] = 0x0D;
            tx.buff[1] = 0x55;	//主机-->设备
            tx.buff[0x0A] = 0x02;//cnt
            tx.buff[0x0B] = 0x6F;            
//             if (mode == EZSMode.NEC)
//             {
//                 tx.buff[0x0C] = DTnec[step];
//             }
//             else if (mode == EZSMode.MOT)
//             {
//                 tx.buff[0x0C] = DTmot[step];
//             }
            tx.buff[0x0C] = (byte)step; 
            byte tmp_cmd = tx.buff[0x0C];

            
            rx = DeviceInteraction(tx);
            if (rx == null)
                return HIDResult.USBError;
            else if (rx.CheckDataList() == false)
            {
                return HIDResult.RxError;
            }
            else if (rx.buff[0x01] == 0x01)
            {
                return HIDResult.ReadEZSError1;
            }
            else if (rx.buff[0x01] == 0x02)
            {
                return HIDResult.ReadEZSError2;
            }
            else if (rx.buff[0x0B] != 0x70)
            {
                return HIDResult.ReadEZSError3;
            }
//             else if (rx.buff[0x0C] != tmp_cmd)
//             {
//                 return HIDResult.ReadEZSError4;
//             }
            else if (rx.buff[1] == 0x00)
            {
                return HIDResult.OK;
            }
            else
            {
                return HIDResult.ReadEZSError5;
            }
        }
        public static byte[,] CANInfo_AT=new byte[0x09,0x10]
        {
		    {0x00,0x0B,0xD0,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
		    {0x01,0x03,0x80,0x00,0x00,0x08,0x02,0x3E,0x02,0xFF,0xFF,0xFF,0xFF,0xFF,0x11,0x00},
		    {0x01,0x03,0x80,0x00,0x00,0x08,0x02,0x3E,0x02,0xFF,0xFF,0xFF,0xFF,0xFF,0x0A,0x00},
		    {0x01,0x9C,0x00,0xBF,0xE0,0x08,0x02,0x1A,0x87,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x01},
		    {0x01,0x9C,0x00,0xBF,0xE0,0x08,0x30,0x08,0x28,0x00,0x00,0x00,0x00,0x00,0x00,0x03},
		    {0x01,0x03,0x80,0x00,0x00,0x08,0x02,0x3E,0x02,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00},
		    {0x01,0x9C,0x00,0xBF,0xE0,0x08,0x04,0x31,0x22,0x6F,0x00,0xFF,0xFF,0xFF,0x00,0x01},
		    {0x01,0x9C,0x00,0xBF,0xE0,0x08,0x30,0x08,0x28,0x00,0x00,0x00,0x00,0x00,0x00,0x01},
		    {0x02,0x9C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
        };
        public HIDResult CANInfoDataStep(int step, out InteractionData rx)
        {
            InteractionData tx = new InteractionData();

            tx.buff[0] = 0x14;
            tx.buff[1] = 0x55;	//主机-->设备 
            tx.buff[4]=0x20;
            for(int i=0;i<0x10;i++)
			{
				tx.buff[i+0x10]=CANInfo_AT[step,i];
			}
            rx = DeviceInteraction(tx);
            if (rx == null)
                return HIDResult.USBError;
            else if (rx.CheckDataList() == false)
            {
                return HIDResult.RxError;
            }
            else if (rx.buff[1] == 0x00)
            {
                return HIDResult.OK;
            }
            else
            {
                return HIDResult.CANErr;
            }
        }
        public static byte[] CANEeprom_Cmd00=new byte[0x24]
        {
            0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
			0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,
			0x20,0x21,0x23,0x26
        };

	    public static byte[,] CANEeprom_a=new byte[0x02,0x10]
        {
		    {0x00,0x0B,0xD0,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
		    {0x01,0x03,0x80,0xBF,0xE0,0x08,0x02,0x3E,0x02,0xFF,0xFF,0xFF,0xFF,0xFF,0x11,0x00},
	    };
	    public static byte[,] CANEeprom_b=new byte[0x03,0x10]
        {
		    {0x01,0x03,0x80,0xBF,0xE0,0x08,0x02,0x3E,0x02,0xFF,0xFF,0xFF,0xFF,0xFF,0x0A,0x00},
		    {0x01,0x9C,0x00,0xBF,0xE0,0x08,0x04,0x31,0x22,0x6F,0x00,0xFF,0xFF,0xFF,0x00,0x01},
		    {0x01,0x9C,0x00,0xBF,0xE0,0x08,0x30,0x08,0x28,0x00,0x00,0x00,0x00,0x00,0x00,0x01},
	    };
	    public static byte[,] CANEeprom_c=new byte[0x04,0x10]
        {
		    {0x01,0x03,0x80,0xBF,0xE0,0x08,0x02,0x3E,0x02,0xFF,0xFF,0xFF,0xFF,0xFF,0x0A,0x00},
		    {0x01,0x9C,0x00,0xBF,0xE0,0x08,0x02,0x21,0x05,0x6F,0x26,0xFF,0xFF,0xFF,0x00,0x01},
		    {0x01,0x9C,0x00,0xBF,0xE0,0x08,0x30,0x08,0x28,0x00,0x00,0x00,0x00,0x00,0x00,0x02},
		    {0x02,0x9C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
	    };
        public HIDResult CANEeprom1Step(int step, out InteractionData rx)
        {
            InteractionData tx = new InteractionData();

            tx.buff[0] = 0x14;
            tx.buff[1] = 0x55;	//主机-->设备 
            tx.buff[4]=0x20;
            for(int i=0;i<0x10;i++)
			{
				tx.buff[i+0x10]=CANEeprom_a[step,i];
			}
            rx = DeviceInteraction(tx);
            if (rx == null)
                return HIDResult.USBError;
            else if (rx.CheckDataList() == false)
            {
                return HIDResult.RxError;
            }
            else if (rx.buff[1] == 0x00)
            {
                return HIDResult.OK;
            }
            else
            {
                return HIDResult.CANErr;
            }
        }
        public HIDResult CANEeprom2Step(int step, out InteractionData rx, out int rxCount)
        {
            InteractionData tx = new InteractionData();

            tx.buff[0] = 0x14;
            tx.buff[1] = 0x55;	//主机-->设备 
            tx.buff[4] = 0x20;
            for (int i = 0; i < 0x10; i++)
            {
                tx.buff[i + 0x10] = CANEeprom_b[step%3, i];
            }
            rxCount = CANEeprom_b[step % 3, 0x0f];
            if (0x01 == (step % 3))
            {
                tx.buff[0x0A + 0x10] = CANEeprom_Cmd00[step / 3];
            }            
            rx = DeviceInteraction(tx);
            if (rx == null)
                return HIDResult.USBError;
            else if (rx.CheckDataList() == false)
            {
                return HIDResult.RxError;
            }
            else if (rx.buff[1] == 0x00)
            {
                return HIDResult.OK;
            }
            else
            {
                return HIDResult.CANErr;
            }
        }
        public HIDResult CANEeprom3Step(int step, out InteractionData rx, out int rxCount)
        {
            InteractionData tx = new InteractionData();

            tx.buff[0] = 0x14;
            tx.buff[1] = 0x55;	//主机-->设备 
            tx.buff[4] = 0x20;
            for (int i = 0; i < 0x10; i++)
            {
                tx.buff[i + 0x10] = CANEeprom_c[step, i];
            }
            rxCount = CANEeprom_c[step, 0x0f];
            rx = DeviceInteraction(tx);
            if (rx == null)
                return HIDResult.USBError;
            else if (rx.CheckDataList() == false)
            {
                return HIDResult.RxError;
            }
            else if (rx.buff[1] == 0x00)
            {
                return HIDResult.OK;
            }
            else
            {
                return HIDResult.CANErr;
            }
        }
        public static byte[,] CANwr_a=new byte[0x06,0x10]
        {
		    {0x00,0x0B,0xD0,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
		    {0x01,0x03,0x80,0xBF,0xE0,0x08,0x02,0x3E,0x02,0xFF,0xFF,0xFF,0xFF,0xFF,0x11,0x00},
		    {0x01,0x03,0x80,0x00,0x00,0x08,0x02,0x3E,0x02,0xFF,0xFF,0xFF,0xFF,0xFF,0x0A,0x00},
		    {0x01,0x9C,0x00,0xBF,0xE0,0x08,0x10,0x0B,0x31,0x22,0x0A,0x7C,0xFE,0xD6,0x00,0x01},
		    {0x01,0x9C,0x00,0xBF,0xE0,0x08,0x21,0x5C,0x68,0xFF,0x98,0x9C,0x00,0x00,0x00,0x01},
		    {0x01,0x9C,0x00,0xBF,0xE0,0x08,0x30,0x08,0x28,0x00,0x00,0x00,0x00,0x00,0x00,0x01},
	    };
	    public static byte[,] CANwr_b=new byte[0x03,0x10]
        {
		    {0x03,0x1C,0xF0,0xC8,0x61,0x90,0x8F,0x00,0x00,0x00,0x1C,0x00,0x00,0x0A,0x11,0x01},
		    {0x01,0x9C,0x00,0xBF,0xE0,0x08,0x03,0x31,0x22,0x0B,0x7C,0xFE,0xD6,0x5C,0x00,0x01},
		    {0x01,0x9C,0x00,0xBF,0xE0,0x08,0x30,0x08,0x28,0x00,0x00,0x00,0x00,0x00,0x00,0x01},
	    };
	    public static byte[,] CANwr_bChk=new byte[0x05,0x10]
        {
		    {0x01,0xAA,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
		    {0x08,0x10,0x0C,0x71,0x22,0x73,0x0B,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
		    {0x07,0x21,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
		    {0x07,0x03,0x7F,0x31,0x78,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
		    {0x08,0x55,0x10,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},//c check
	    };

	    public static byte[,] CANwr_c=new byte[0x03,0x10]
        {
		    {0x01,0x9C,0x00,0xBF,0xE0,0x08,0x03,0x31,0x22,0x0B,0x7C,0xFE,0xD6,0x5C,0x10,0x00},
		    {0x03,0x1C,0xF0,0xC8,0x61,0x90,0x8F,0x00,0x00,0x00,0x1C,0x00,0x00,0x0A,0x1E,0x01},
		    {0x03,0x1C,0xF0,0xC8,0x61,0x90,0x8F,0x00,0x00,0x00,0x1C,0x00,0x00,0x0A,0x1E,0x01},
	    };
	    public static byte[,] CANwr_d=new byte[0x04,0x10]
        {
		    {0x01,0x9C,0x00,0xBF,0xE0,0x08,0x03,0x31,0x22,0x0B,0x7C,0xFE,0xD6,0x5C,0x10,0x00},
		    {0x04,0x1C,0x00,0x48,0x45,0xD8,0x26,0x81,0xB0,0x5D,0x63,0x00,0x00,0x00,0x1E,0x01},
		    {0x03,0x1C,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x0A,0x1E,0x01},
		    {0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},

	    };
        public byte[] DTwr = new byte[0x0c*0x08];
        public static byte[,] CANwr_e = new byte[0x04, 0x10]
        {
		    {0x05,0x1C,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1E,0x01},
		    {0x03,0x1C,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x0A,0x20,0x01},
		    {0x01,0x9C,0x00,0xBF,0xE0,0x08,0x30,0x08,0x28,0x00,0x00,0x00,0x00,0x00,0x00,0x01},
		    {0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
	    };
        public HIDResult CANWrite1Step(int step, out InteractionData rx)
        {
            InteractionData tx = new InteractionData();

            tx.buff[0] = 0x14;
            tx.buff[1] = 0x55;	//主机-->设备 
            tx.buff[4] = 0x20;
            for (int i = 0; i < 0x10; i++)
            {
                tx.buff[i + 0x10] = CANwr_a[step, i];
            }
            rx = DeviceInteraction(tx);
            if (rx == null)
                return HIDResult.USBError;
            else if (rx.CheckDataList() == false)
            {
                return HIDResult.RxError;
            }
            else if (rx.buff[1] == 0x00)
            {
                return HIDResult.OK;
            }
            else
            {
                return HIDResult.CANErr;
            }
        }
        public HIDResult CANWrite2Step(int step, out InteractionData rx)
        {
            InteractionData tx = new InteractionData();

            tx.buff[0] = 0x14;
            tx.buff[1] = 0x55;	//主机-->设备 
            tx.buff[4] = 0x20;
            for (int i = 0; i < 0x10; i++)
            {
                tx.buff[i + 0x10] = CANwr_b[step, i];
            }
            rx = DeviceInteraction(tx);
            if (rx == null)
                return HIDResult.USBError;
            else if (rx.CheckDataList() == false)
            {
                return HIDResult.RxError;
            }
            else if (rx.buff[1] == 0x00)
            {
                return HIDResult.OK;
            }
            else if (rx.buff[1] == 0x03)
            {
                return HIDResult.CANWriteWait;
            }
            else
            {
                return HIDResult.CANErr;
            }
        }
        public HIDResult CANWrite3Step(int step, out InteractionData rx)
        {
            InteractionData tx = new InteractionData();

            tx.buff[0] = 0x14;
            tx.buff[1] = 0x55;	//主机-->设备 
            tx.buff[4] = 0x20;
            for (int i = 0; i < 0x10; i++)
            {
                tx.buff[i + 0x10] = CANwr_c[step, i];
            }
            rx = DeviceInteraction(tx);
            if (rx == null)
                return HIDResult.USBError;
            else if (rx.CheckDataList() == false)
            {
                return HIDResult.RxError;
            }
            else if (rx.buff[1] == 0x00)
            {
                return HIDResult.OK;
            }
            else if (rx.buff[1] == 0x03)
            {
                return HIDResult.CANWriteWait;
            }
            else
            {
                return HIDResult.CANErr;
            }
        }
        public static int DTwrIndex = 0;
        public HIDResult CANWrite4Step(int step, out InteractionData rx)
        {
            InteractionData tx = new InteractionData();
            
            tx.buff[0] = 0x14;
            tx.buff[1] = 0x55;	//主机-->设备 
            tx.buff[4] = 0x20;
            for (int i = 0; i < 0x10; i++)
            {
                tx.buff[i + 0x10] = CANwr_d[step%2, i];
            }
            if (step % 2 == 1)
            {
                for (int i = 0; i < 0x08; i++)
                {
                    tx.buff[i + 0x13] = DTwr[0x08 * DTwrIndex + i];//DATAwr[k][i];
                }
                tx.buff[0x12] = (byte)DTwrIndex;
                DTwrIndex++;
            }
            rx = DeviceInteraction(tx);
            if (rx == null)
                return HIDResult.USBError;
            else if (rx.CheckDataList() == false)
            {
                return HIDResult.RxError;
            }
            else if (rx.buff[1] == 0x00)
            {
                return HIDResult.OK;
            }
            else if (rx.buff[1] == 0x03)
            {
                return HIDResult.CANWriteWait;
            }
            else
            {
                return HIDResult.CANErr;
            }
        }
        public HIDResult CANWrite5Step(int step, out InteractionData rx)
        {
            InteractionData tx = new InteractionData();

            tx.buff[0] = 0x14;
            tx.buff[1] = 0x55;	//主机-->设备 
            tx.buff[4] = 0x20;
            for (int i = 0; i < 0x10; i++)
            {
                tx.buff[i + 0x10] = CANwr_e[step, i];
            }           
            rx = DeviceInteraction(tx);
            if (rx == null)
                return HIDResult.USBError;
            else if (rx.CheckDataList() == false)
            {
                return HIDResult.RxError;
            }
            else if (rx.buff[1] == 0x00)
            {
                return HIDResult.OK;
            }
            else if (rx.buff[1] == 0x03)
            {
                return HIDResult.CANWriteWait;
            }
            else
            {
                return HIDResult.CANErr;
            }
        }
    }
    public enum EZSMode
    {
        NEC,
        MOT
    }
}
